package com.fuping;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class UtilMethod {

	/**
	 * 判断文件是否存在
	 * @param path
	 * @return
	 */
	public static boolean isExists(String path) {
		if (path != null) {

			File file = new File(path);
			return file.exists();
		} else {
			return false;
		}

	}

	public static List<String> readFile(String path) {
		List<String> lt = new ArrayList<String>();
		File file = new File(path);
		FileReader reader = null;
		BufferedReader bf = null;

		if (file.exists()) {
			try {
				reader = new FileReader(file);
				bf = new BufferedReader(reader);
				String line = null;
				while ((line = bf.readLine()) != null) {
					if (line.trim().length() > 0) {
						lt.add(line.trim());
					}
				}
				bf.close();
				reader.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return lt;
	}

	public static void writeResult(String path, String content, boolean flag) {

		File f = new File(path);
		FileWriter fw = null;
		try {
			if (!f.exists()) {
				f.createNewFile();
			}
			fw = new FileWriter(f, flag);
			fw.write(content + "\r\n");
			fw.flush();
			fw.close();
		} catch (IOException e) {

		}
	}
}
